package device

import (
	"context"
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/device"
	deviceReq "github.com/flipped-aurora/gin-vue-admin/server/model/device/request"
	"github.com/flipped-aurora/gin-vue-admin/server/model/example"
)

type ModelService struct{}

// CreateModel 创建model表记录
// Author [yourname](https://github.com/yourname)
func (modelService *ModelService) CreateModel(ctx context.Context, model *device.Model) (err error) {
	err = global.GVA_DB.Create(model).Error
	return err
}

// DeleteModel 删除model表记录
// Author [yourname](https://github.com/yourname)
func (modelService *ModelService) DeleteModel(ctx context.Context, ID string) (err error) {
	err = global.GVA_DB.Delete(&device.Model{}, "id = ?", ID).Error
	return err
}

// DeleteModelByIds 批量删除model表记录
// Author [yourname](https://github.com/yourname)
func (modelService *ModelService) DeleteModelByIds(ctx context.Context, IDs []string) (err error) {
	err = global.GVA_DB.Delete(&[]device.Model{}, "id in ?", IDs).Error
	return err
}

// UpdateModel 更新model表记录
// Author [yourname](https://github.com/yourname)
func (modelService *ModelService) UpdateModel(ctx context.Context, model device.Model) (err error) {
	err = global.GVA_DB.Model(&device.Model{}).Where("id = ?", model.ID).Updates(&model).Error
	return err
}

// GetModel 根据ID获取model表记录
// Author [yourname](https://github.com/yourname)
func (modelService *ModelService) GetModel(ctx context.Context, ID string) (model device.Model, err error) {
	err = global.GVA_DB.Where("id = ?", ID).First(&model).Error
	return
}

// GetModelInfoList 分页获取model表记录
// Author [yourname](https://github.com/yourname)
func (modelService *ModelService) GetModelInfoList(ctx context.Context, info deviceReq.ModelSearch) (list []device.Model, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&device.Model{})
	var models []device.Model
	// 如果有条件搜索 下方会自动创建搜索语句
	if len(info.CreatedAtRange) == 2 {
		db = db.Where("created_at BETWEEN ? AND ?", info.CreatedAtRange[0], info.CreatedAtRange[1])
	}

	if info.EquipmentModel != nil && *info.EquipmentModel != "" {
		db = db.Where("equipment_model LIKE ?", "%"+*info.EquipmentModel+"%")
	}
	if info.DeviceName != nil && *info.DeviceName != "" {
		db = db.Where("device_name LIKE ?", "%"+*info.DeviceName+"%")
	}
	if info.Size != nil && *info.Size != "" {
		db = db.Where("size = ?", *info.Size)
	}
	if info.Weight != nil && *info.Weight != "" {
		db = db.Where("weight = ?", *info.Weight)
	}
	if info.CertificateOfRegistration != nil && *info.CertificateOfRegistration != "" {
		db = db.Where("certificate_of_registration LIKE ?", "%"+*info.CertificateOfRegistration+"%")
	}
	if info.ListingDate != nil {
		db = db.Where("listing_date = ?", *info.ListingDate)
	}
	if info.Describe != nil && *info.Describe != "" {
		db = db.Where("describe LIKE ?", "%"+*info.Describe+"%")
	}
	if info.Image != "" {
		// TODO 数据类型为复杂类型，请根据业务需求自行实现复杂类型的查询业务
	}
	err = db.Count(&total).Error
	if err != nil {
		return
	}

	if limit != 0 {
		db = db.Limit(limit).Offset(offset)
	}

	err = db.Find(&models).Error
	return models, total, err
}
func (modelService *ModelService) GetModelPublic(ctx context.Context) {
	// 此方法为获取数据源定义的数据
	// 请自行实现
}

// Upload 创建文件上传记录
// Author [yourname](https://github.com/yourname)
func (modelService *ModelService) Upload(file example.ExaFileUploadAndDownload) error {
	return global.GVA_DB.Create(&file).Error
}
